package cn.com.jslh.boarpro.main.view.fragment;

import android.Manifest;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.model.DeliveryRecordModelImpl;
import cn.com.jslh.boarpro.main.presenter.AddDeliveryRecordPresenterImpl;
import cn.com.jslh.boarpro.util.InputDialogUtil;
import cn.com.jslh.boarpro.util.ScanDialog;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 分娩记录新增
 * 作者：周洲 on 2017/7/26 10:42
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class AddDeliveryRecordFragment extends BaseFragment implements BaseListView {
    @BindView(R.id.muzhuTextView)
    TextView muzhuTextView;
    @BindView(R.id.muzhuInput)
    ImageView muzhuInput;
    @BindView(R.id.zhongmiaoshu)
    EditText zhongmiaoshu;
    @BindView(R.id.ruozaishu)
    EditText ruozaishu;
    @BindView(R.id.wozhong)
    EditText wozhong;
    @BindView(R.id.sitaoshu)
    EditText sitaoshu;
    @BindView(R.id.munaiyishu)
    EditText munaiyishu;
    @BindView(R.id.jianzaishu)
    EditText jianzaishu;
    @BindView(R.id.jizaishu)
    EditText jizaishu;
    @BindView(R.id.subbtn)
    Button subbtn;
    @BindView(R.id.fragment_layout)
    FrameLayout fragmentLayout;
    private AddDeliveryRecordPresenterImpl addDeliveryRecordPresenter;
    private Bundle savedInstanceState;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            this.savedInstanceState = savedInstanceState;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        new RxPermissions(getActivity())
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (!aBoolean) {
                            Toast.makeText(getContext(), "未获取摄像头权限无法使用扫描二维码功能", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        View view = inflater.inflate(R.layout.add_delivery_record_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        addDeliveryRecordPresenter = new AddDeliveryRecordPresenterImpl(getContext(), this);
        InfoShowFragment.initInfoShowFragment(getFragmentManager());
        return view;
    }

    @Override
    public void onItemClicked(String... params) { //数据保存成功
        clearui();
    }


    @OnClick({R.id.muzhuTextView, R.id.muzhuInput, R.id.subbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.muzhuTextView:
                //弹出扫描框，扫描结果放入母猪textView
                if (DeliveryRecordModelImpl.zhusheid.equals("")) {
                    Toast.makeText(getContext(), "请先在记录界面选择猪舍", Toast.LENGTH_SHORT).show();
                    return;
                }
                new ScanDialog.Builder(getContext())
                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                            @Override
                            public void onQRCodeRead(String text, PointF[] points) { //耳号
                                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                                InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                                    @Override
                                    public void success(String s, String id) {
                                        muzhuTextView.setText(s);
                                        muzhuTextView.setTag(id);
                                    }
                                }, InfoShowFragment.FenMianType, text, SPUtils.getInstance().getString(BaseApplication.FLINEID), DeliveryRecordModelImpl.zhusheid);//生产线,猪舍ID
                            }
                        }).create().show();
                break;
            case R.id.muzhuInput:
                if (DeliveryRecordModelImpl.zhusheid.equals("")) {
                    Toast.makeText(getContext(), "请先在记录界面选择猪舍", Toast.LENGTH_SHORT).show();
                    return;
                }
                InputDialogUtil.showInputDialog(getContext(), "请输入耳号", muzhuTextView.getText().toString(), new InputDialogUtil.DialogListener() {
                    @Override
                    public void getText(String s) {
                        InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                            @Override
                            public void success(String s, String id) {
                                muzhuTextView.setText(s);
                                muzhuTextView.setTag(id);
                            }
                        }, InfoShowFragment.FenMianType, s, SPUtils.getInstance().getString(BaseApplication.FLINEID), DeliveryRecordModelImpl.zhusheid);//猪舍ID
                    }
                });
                break;
            case R.id.subbtn://提交
//                "muZhuID": "kvYAAAgGbu/ECiku",--母猪ID
                if (muzhuTextView.getTag() == null) {
                    Toast.makeText(getContext(), "母猪耳号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
//                "pigletQty": 2,--种苗数
                if (zhongmiaoshu.getText().toString().equals("")) {
                    zhongmiaoshu.setText("0");
                }
//                "jianZaiQty": 10,--健仔数
                if (jianzaishu.getText().toString().equals("")) {
//                    jianzaishu.setText("0");
                    Toast.makeText(getContext(), "健仔数必填", Toast.LENGTH_SHORT).show();
                    return;
                }
//                "ruoZaiQty": 3,--弱仔数
                if (ruozaishu.getText().toString().equals("")) {
                    ruozaishu.setText("0");
                }
//                "siTaiQty": 1,--死淘数
                if (sitaoshu.getText().toString().equals("")) {
                    sitaoshu.setText("0");
                }
//                "jiZaiQty": 0,--畸仔数--如果为空，传过来0
                if (jizaishu.getText().toString().equals("")) {
                    jizaishu.setText("0");
                }
//                "muNaiYiQty":1,--木乃伊数
                if (munaiyishu.getText().toString().equals("")) {
                    munaiyishu.setText("0");
                }
//                "woWt":105.55--窝重
                if (wozhong.getText().toString().equals("")) {
//                    wozhong.setText("0");
                    Toast.makeText(getContext(), "窝重必填", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!addDeliveryRecordPresenter.subBreedingRecordItemInfo(
                        muzhuTextView.getTag().toString(),
                        zhongmiaoshu.getText().toString(),
                        jianzaishu.getText().toString(),
                        ruozaishu.getText().toString(),
                        sitaoshu.getText().toString(),
                        jizaishu.getText().toString(),
                        munaiyishu.getText().toString(),
                        wozhong.getText().toString(),
                        muzhuTextView.getText().toString()
                )) {
                    muzhuTextView.setText("请点击扫描录入");
                    muzhuTextView.setTag(null);
                }
                //clear ui
                break;
        }
    }

    private void clearui() {
        muzhuTextView.setText("请点击扫描录入");
        zhongmiaoshu.setText("");
        jianzaishu.setText("");
        ruozaishu.setText("");
        sitaoshu.setText("");
        jizaishu.setText("");
        munaiyishu.setText("");
        wozhong.setText("");
        InfoShowFragment.getPigInfo(null, InfoShowFragment.Clear);
        DeliveryRecordFragment.FlashUI();//更新UI
    }

}
